﻿using System;
using System.Device.Gpio;
using System.Threading;
using Iot.Device.Hcsr04;

using Sang.IoT.SSD1306;
using SkiaSharp;
namespace IotDeviceToolHepler.Device
{
    /// <summary>
    /// Hcsr04超声波测距
    /// </summary>
    public class DeviceDisplay_Hcsr04
    {
        //https://blog.csdn.net/weixin_43288375/article/details/131148818
        /// <summary>
        /// https://cloud.tencent.com/developer/article/2167580?areaSource=102001.7&traceId=4btjeE-8zxMvzfTF_T0VN
        /// </summary>
        public void Test()
        {
            Display();
        }
      
        public void Display()
        {
            Console.WriteLine("Test sensor. Press Ctrl+C to end.");
            // 此处注意 使用的是TX2设备，其他设备需查阅转换
            int TRIG_GPIO = 394; //Pin 38
            int ECHO_GPIO = 393; //Pin 40
            using var controller = new GpioController();
            using var sensor = new Hcsr04(controller, TRIG_GPIO, ECHO_GPIO);

            while (true)
            {
                if (sensor.TryGetDistance(out _))
                {
                    Console.WriteLine($"Distance: {sensor.Distance}");
                }
                Thread.Sleep(1000);
            }
        }

    }
}
